<?php

use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;

class Admin extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $exists = $this->hasTable('admin');
        if(!$exists){
        $table = $this->table('admin', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '管理员' ,'id' => 'id' ,'primary_key' => ['id']]);
        $table->addColumn('account', 'string', ['limit' => 60,'null' => false,'default' => '','signed' => true,'comment' => '账号',])
			->addColumn('realname', 'string', ['limit' => 50,'null' => false,'default' => null,'signed' => true,'comment' => '姓名',])
			->addColumn('password', 'string', ['limit' => 60,'null' => false,'default' => '','signed' => true,'comment' => '密码',])
			->addColumn('salt', 'string', ['limit' => 30,'null' => false,'default' => '','signed' => true,'comment' => '密码盐',])
			->addColumn('avatar', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => true,'comment' => '头像',])
			->addColumn('email', 'string', ['limit' => 60,'null' => true,'signed' => true,'comment' => '邮箱',])
			->addColumn('login_fail', 'boolean', ['null' => false,'default' => 0,'signed' => false,'comment' => '登录失败次数',])
			->addColumn('login_ip', 'string', ['limit' => 20,'null' => true,'signed' => true,'comment' => '登录 ip',])
			->addColumn('is_super', 'enum', ['values' => ['1','0']])
			->addColumn('status', 'enum', ['values' => ['normal','disabled']])
			->addColumn('login_time', 'integer', ['limit' => MysqlAdapter::INT_BIG,'null' => true,'signed' => true,'comment' => '登录时间',])
			->addColumn('create_time', 'integer', ['limit' => MysqlAdapter::INT_BIG,'null' => true,'signed' => true,'comment' => '创建时间',])
			->addColumn('update_time', 'integer', ['limit' => MysqlAdapter::INT_BIG,'null' => true,'signed' => true,'comment' => '更新时间',])
			->addIndex(['email'], ['unique' => true,'name' => 'email'])
			->addIndex(['account'], ['unique' => true,'name' => 'account'])
            ->create();
        }
    }
}
